package com.web.admin.web.cms.adv;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.ad.facade.AdvPosFacade;
import com.web.cms.ad.message.AdvPosRequest;
import com.web.cms.ad.message.AdvPosResponse;
import com.web.cms.doc.facade.WebFacade;
import com.web.cms.doc.message.WebResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * 广告位置控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年05月23日
 */
@Controller("advAdvPosController")
@Http(url = "/cms/adv")
public class AdvPosController
{
	@Autowired
	private AdvPosFacade advPosFacade;
	@Autowired
	private WebFacade webFacade;
	
	private DictParser parser = DictParser.getInstance();
	
	public AdvPosController()
	{
	}

	@Http(url = "/adv_pos_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		AdvPosRequest advPosRequest = request.getQueryObject(AdvPosRequest.class);
		List<WebResponse> webList = webFacade.listAll();
		if (advPosRequest.getWebId() == null && !webList.isEmpty())
		{
			advPosRequest.setWebId(webList.get(0).getId());
		}
		return View.createBuilder().setView("/cms/adv/adv_pos_list")
				.put("model", advPosRequest)
				.put("page", advPosFacade.listPage(advPosRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.put("map", parser.getStringMap())
				.put("webList", webList)
				.build();
	}
	
	@Http(url = "/adv_pos_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		AdvPosRequest advPosRequest = request.getPostObject(AdvPosRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/cms/adv/adv_pos_list_grid")
				.put("page", advPosFacade.listPage(advPosRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/adv_pos_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		AdvPosRequest advPosRequest = request.getQueryObject(AdvPosRequest.class);
		List<WebResponse> webList = webFacade.listAll();
		AdvPosResponse response = null;
		if (advPosRequest.getId() != null)
		{
			response = advPosFacade.get(advPosRequest.getId());
		}
		if (response == null)
		{
			response = new AdvPosResponse();
			response.setWebId(advPosRequest.getWebId());
		}
		return View.createBuilder().setView("/cms/adv/adv_pos_get")
				.put("model", response)
				.put("webList", webList)
				.put("map", parser.getStringMap())
				.build();
	}
	
	@Http(url = "/adv_pos_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		AdvPosRequest advPosRequest = request.getPostObject(AdvPosRequest.class);
		AdvPosResponse response = null;
		if (advPosRequest.getId() == null)
		{
			ValidationUtil.valid(advPosRequest, SaveModel.class);
			response = advPosFacade.add(advPosRequest);
		}
		else
		{
			ValidationUtil.valid(advPosRequest, UpdateModel.class);
			response = advPosFacade.update(advPosRequest);
		}
	}
	
}
